//
//  NetWorkTool.m
//  BabyCloud_Parent
//
//  Created by mac on 2017/11/29.
//  Copyright © 2017年 SZJ. All rights reserved.
//

#import "NetWorkTool.h"
#import "NetErrorView.h"
#import "AFNetworking.h"


@implementation NetWorkTool

+ (void)get:(NSString *)urlStr withSuccess:(success)success withFails:(fails)fails{

    
    NSString *url = [NSString stringWithFormat:@"%@%@",header,urlStr];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    url =  [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    

    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];//转换数据格式
        NSString * json = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        dispatch_async(dispatch_get_main_queue(), ^{
            success(url,dic,json,@{});
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         dispatch_async(dispatch_get_main_queue(), ^{

            fails(url,error,@{});
        });
    }];
}

+ (void)post:(NSString *)urlStr withParams:(NSDictionary *)params withSuccess:(success)success withFails:(fails)fails{
    
   
    
    NSString *url = [NSString stringWithFormat:@"%@%@",header,urlStr];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    

    url =  [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];//转换数据格式
        NSString * json = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        dispatch_async(dispatch_get_main_queue(), ^{
      
            success(url,dic,json,params);
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
      
            fails(url,error,params);
#ifdef DEBUG
            [NetErrorView shareWithError:error withUrl:url withParams:params];
#endif
        });
    }];
}

+ (void)upLoad:(NSString *)urlStr withParams:(NSDictionary *)params withImageArray:(NSArray <UIImage *> *)imageArray withNameArr:(NSArray <NSString  *> *)nameArr withSuccess:(success)success withFails:(fails)fails{
    
    if (imageArray.count != nameArr.count) {
         NSLog(@"\n 警告 \n %@ 这个接口的传的图片数量和图片字段数量不一致 有可能导致崩溃 ",urlStr);
    }
    
    NSString *url = [NSString stringWithFormat:@"%@%@",header,urlStr];


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager.securityPolicy setValidatesDomainName:NO];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    url =  [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //上传文件参数
        int index = 0;
        for (UIImage *image in imageArray) {
            NSData *imageData= UIImageJPEGRepresentation(image, 0.5);
            //这个就是参数
            NSString *nameStr = nameArr[index];
            NSString *fileNameArr = [NSString stringWithFormat:@"%@.jpg",nameStr];
            [formData appendPartWithFileData:imageData name:nameStr fileName:fileNameArr mimeType:@"image/jpeg"];
            index++;
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];//转换数据格式
        NSString * json = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        dispatch_async(dispatch_get_main_queue(), ^{
       
            success(url,dic,json,params);
        });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            fails(url,error,params);
#ifdef DEBUG
            
            NSError *underError = error.userInfo[@"NSUnderlyingError"];
            
            NSData *responseData = error.userInfo[@"com.alamofire.serialization.response.error.data"];
            
            NSString *result = [[NSString alloc] initWithData:responseData  encoding:NSUTF8StringEncoding];
            
            
            UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            [[UIApplication sharedApplication].windows.firstObject addSubview:webView];
            [webView loadHTMLString:result baseURL:nil];
            
#endif
        });
    }];
}




@end
